package com.fc.advbox.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fc.advbox.dao.EquipmentAttrDao;
import com.fc.advbox.dao.EquipmentDao;
import com.fc.advbox.dto.equipment.QueryEquipmentRequest;
import com.fc.advbox.dto.equipment.SimpleEquipmentDto;
import com.fc.advbox.dto.report.EquipmentRegion;
import com.fc.advbox.entity.AdvOrgEntity;
import com.fc.advbox.entity.EquipmentAttrEntity;
import com.fc.advbox.entity.EquipmentEntity;
import com.fc.advbox.entity.EquipmentHoldingEntity;
import com.fc.advbox.service.IEquipmentHoldingService;
import com.fc.advbox.service.IEquipmentService;
import com.fc.advbox.service.IOrgService;
import com.fc.advbox.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("equipmentService")
public class EquipmentServiceImpl implements IEquipmentService {

    @Autowired
    private EquipmentDao equipmentDao;

    @Autowired
    private EquipmentAttrDao equipmentAttrDao;

    @Autowired
    private IEquipmentHoldingService equipmentHoldingService;

    @Autowired
    private IOrgService orgService;

    @Override
    public List<EquipmentEntity> queryList(Map<String, Object> params) {
        return equipmentDao.queryList(params);
    }

    @Override
    public EquipmentEntity queryObject(Long id) {
        EquipmentEntity equipment = equipmentDao.queryObject(id);
        if(equipment != null)
        {
            // 获取该设备额外属性
            List<EquipmentAttrEntity> equipmentAttrs = equipmentAttrDao.queryList(equipment.getId());
            if(equipmentAttrs != null && !equipmentAttrs.isEmpty())
            {
                equipment.setAttrs(equipmentAttrs);
            }
        }
        return equipment;
    }

    @Override
    public void save(EquipmentEntity equipment) {
        EquipmentEntity equipmentEntity = equipmentDao.queryByCode(equipment.getCode());
        if(equipmentEntity != null)
        {
            throw new RRException("设备编码已存在, 请重新输入!");
        }

        // 1. 保存设备信息 到主表
        equipmentDao.save(equipment);

        // 2. 保存设备的额外信息，属性
        if(equipment.getId() != null
                && equipment.getAttrs() != null
                && !equipment.getAttrs().isEmpty())
        {
            for(EquipmentAttrEntity attrEntity : equipment.getAttrs())
            {
                // 赋值设备ID
                attrEntity.setEquipmentId(equipment.getId());
            }
            // 批量保存额外参数
            equipmentAttrDao.saveBatch(equipment.getAttrs());
        }

        // 3. 初始化设备的合同收益数据
        EquipmentHoldingEntity equipHolding = new EquipmentHoldingEntity();
        equipHolding.setEquipmentId(equipment.getId());
        equipHolding.setOwnerOrgId(getDefaultOwnerOrgId()); // 默认设置为 国鑫智能ID
        equipHolding.setOwnerOrgName(getDefaultOwnerOrgName());// 默认设置为 国鑫智能
        equipmentHoldingService.save(equipHolding);
    }

    @Override
    public void update(EquipmentEntity equipment) {
        if(equipment == null || equipment.getId() == null || equipment.getId() == 0)
        {
            return;
        }

        // 1. 更新设备表
        equipmentDao.update(equipment);

        // 2. 清除设备原来的额外属性记录
        equipmentAttrDao.delete(equipment.getId());

        // 3. 重新保存额外的属性记录
        if(equipment.getId() != null
                && equipment.getAttrs() != null
                && !equipment.getAttrs().isEmpty())
        {
            for(EquipmentAttrEntity attrEntity : equipment.getAttrs())
            {
                // 赋值设备ID
                attrEntity.setEquipmentId(equipment.getId());
            }
            // 批量保存额外参数
            equipmentAttrDao.saveBatch(equipment.getAttrs());
        }
    }

    @Override
    public void delete(Long equipmentId) {
        equipmentDao.delete(equipmentId);
    }

    @Override
    public void deleteBatch(Long[] equipmentIds) {
        equipmentDao.deleteBatch(equipmentIds);
    }

    @Override
    public List<SimpleEquipmentDto> querySimpleList(Map<String, Object> params){
        List<EquipmentEntity> eqEntityList = equipmentDao.queryList(params);
        List<SimpleEquipmentDto> eqDtoList= new ArrayList<>();
        for(EquipmentEntity eq:eqEntityList){
            SimpleEquipmentDto dto = new SimpleEquipmentDto();
            dto.setCity(eq.getCity());
            dto.setCode(eq.getCode());
            dto.setDistrict(eq.getDistrict());
            dto.setId(eq.getId());
            dto.setLatitude(eq.getLatitude());
            dto.setLongitude(eq.getLongitude());
            dto.setStatus(eq.getStatus());
            eqDtoList.add(dto);
        }
        return eqDtoList;
    }

    @Override
    public List<String> queryCity(){
        return equipmentDao.queryCity();
    }

    @Override
    public List<EquipmentEntity> queryEquipmentsByContractId(JSONObject params) {
        return equipmentDao.queryEquipmentListByContractId(params);
    }

    @Override
    public List<EquipmentRegion> queryEquipmentRegionReport(JSONObject params) {
        return equipmentDao.queryEquipmentRegionReport(params);
    }

    /**
     * 获取默认ownerOrgId，获取国鑫智能ID
     * @return
     * @throws RRException
     */
    private Long getDefaultOwnerOrgId() throws RRException
    {
        AdvOrgEntity advOrgEntity = orgService.getDefaultOrg();
        if(advOrgEntity == null || advOrgEntity.getId() == null || advOrgEntity.getId() == 0)
        {
            throw new RRException("元数据错误, 请与管理员联系. 未能获取国鑫智能默认ID!");
        }
        return advOrgEntity.getId().longValue();
    }

    private String getDefaultOwnerOrgName() throws RRException
    {
        AdvOrgEntity advOrgEntity = orgService.getDefaultOrg();
        if(advOrgEntity == null || advOrgEntity.getId() == null || advOrgEntity.getId() == 0)
        {
            throw new RRException("元数据错误, 请与管理员联系. 未能获取国鑫智能默认ID!");
        }
        return advOrgEntity.getName();
    }
}
